import {
	EffectComposer
} from 'three/examples/jsm/postprocessing/EffectComposer.js';
import {
	RenderPass
} from 'three/examples/jsm/postprocessing/RenderPass.js';
import {
	UnrealBloomPass
} from 'three/examples/jsm/postprocessing/UnrealBloomPass.js';

import {
	ShaderPass
} from 'three/examples/jsm/postprocessing/ShaderPass.js';

import { AfterimagePass } from 'three/examples/jsm/postprocessing/AfterimagePass.js';

function houQiTeXiao(renderer, scene, camera) {
	const ENTIRE_SCENE = 0,
		BLOOM_SCENE = 1;

	const bloomLayer = new THREE.Layers();
	bloomLayer.set(BLOOM_SCENE); //BLOOM_SCENE发光层

	const darkMaterial = new THREE.MeshBasicMaterial({
		color: "black"
	});
	const materials = {};

	const renderScene = new RenderPass(scene, camera);

	const bloomPass = new UnrealBloomPass(new THREE.Vector2(window.innerWidth, window.innerHeight), 1.5, 0.4, 0.85);
	bloomPass.threshold = 0;
	bloomPass.strength = 1; //0-10
	bloomPass.radius = 0; //0-1

	const bloomComposer = new EffectComposer(renderer);
	bloomComposer.renderToScreen = false;
	bloomComposer.addPass(renderScene);
	bloomComposer.addPass(bloomPass);
	
	//运动幻影
	let afterimagePass = new AfterimagePass();
	bloomComposer.addPass( afterimagePass );
	afterimagePass.uniforms[ "damp" ].value = 0.9;

	let vertexShader = `varying vec2 vUv;
				void main() {
					vUv = uv;
					gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
				}`
	let fragmentShader = `uniform sampler2D baseTexture;
				uniform sampler2D bloomTexture;
				varying vec2 vUv;
				void main() {
					gl_FragColor = ( texture2D( baseTexture, vUv ) + vec4( 1.0 ) * texture2D( bloomTexture, vUv ) );
				}`
	const finalPass = new ShaderPass(
		new THREE.ShaderMaterial({
			uniforms: {
				baseTexture: {
					value: null
				},
				bloomTexture: {
					value: bloomComposer.renderTarget2.texture
				}
			},
			vertexShader,
			fragmentShader,
			defines: {}
		}), "baseTexture"
	);

	finalPass.needsSwap = true;

	const finalComposer = new EffectComposer(renderer);
	finalComposer.addPass(renderScene);
	finalComposer.addPass(finalPass);

	bloomComposer.renderToScreen = false;

	function teXiaoRender() {
		// render scene with bloom
		renderBloom();

		// render the entire scene, then render bloom scene on top
		finalComposer.render();

	}

	function renderBloom() {
		scene.traverse(darkenNonBloomed);
		bloomComposer.render();
		scene.traverse(restoreMaterial);
	}

	function darkenNonBloomed(obj) {
		
		if (obj.isMesh && bloomLayer.test(obj.layers) === false) {
			materials[obj.uuid] = obj.material;
			obj.material = darkMaterial;
		}
	}

	function restoreMaterial(obj) {

		if (materials[obj.uuid]) {
			obj.material = materials[obj.uuid];
			delete materials[obj.uuid];
		}
	}

	// const geometry = new THREE.IcosahedronGeometry(1, 15);

	// const color = new THREE.Color();
	// color.setHSL(Math.random(), 0.7, Math.random() * 0.2 + 0.05);

	// const material = new THREE.MeshBasicMaterial({
	// 	color: color
	// });
	// const sphere = new THREE.Mesh(geometry, material);
	// scene.add(sphere);

	// sphere.layers.enable(BLOOM_SCENE);

	return {
		teXiaoRender
	};
}

export default houQiTeXiao;
